<?php


namespace app\api\controller;

use app\common\lib\Code;
use app\common\lib\Export;
use \think\Exception;
use app\common\lib\Request;
use app\api\model\User_operation;
use app\api\model\User as UserModel;
use app\common\facade\WebSocketToken;

class User extends Base
{
    private $cmd_data = [
        //获取用户信息
        "100"=>"get_info",
        //设置用户信息
        "101"=>"set_info",
        // 关注用户
        "102"=>"focus_on_user",
        // 获取用户websocket登录token
        "103"=>"get_socket_token",
        // 获取关注列表
        "104"=>"get_focus_user",
    ];
    public function index(){
        try{
            // 检验登录情况
            parent::check_user_info();

            $cmd = Request::get('cmd');
            if(!array_key_exists($cmd,$this->cmd_data)) throw new Exception("参数错误");
            $method = $this->cmd_data[$cmd];

            $this->$method();
        }catch (Exception $e){
            Export::exp_err($e->getMessage());
        }

        return '';
    }
    /** 获取用户信息 */
    private function get_info(){
        $user_id = $this->user_id;
        $user = UserModel::get($user_id);
        $info = [
            "data"=>$user
        ];
        Export::exp_ok($info);
    }
    /** 设置用户信息 */
    private function set_info(){
        $data = Request::post();

        $signature = array_key_exists('signature',$data)?$data['signature']:"";
        $birthday = array_key_exists('birthday',$data)?$data['birthday']:"";
        $gender = array_key_exists('gender',$data)?$data['gender']:"";
        $address = array_key_exists('address',$data)?$data['address']:"";

        $old_password = array_key_exists('old_password',$data)?$data['old_password']:"";
        $new_password = array_key_exists('new_password',$data)?$data['new_password']:"";

        $mobile = array_key_exists('mobile',$data)?$data['mobile']:"";
        $email = array_key_exists('email',$data)?$data['email']:"";
        $code = array_key_exists('code',$data)?$data['code']:"";

        $user = UserModel::get($this->user_id);
        $signature && $user->signature      = $signature;
        $birthday && $user->birthday        = $birthday;
        $gender && $user->gender         = $gender;
        $address && $user->address     = $address;

        // 设置密码
        if($old_password){
            if($user['password'] != md5($old_password)){
                throw new Exception("密码不正确！");
            }
            if(!$new_password) throw new Exception("密码不能为空！");
            $user->password     = $new_password;
        }

        // 设置手机号，邮箱
        if(($mobile || $email) && !$code){
            throw new Exception("请输入验证码！");
        }

        if($mobile){
            $key = md5($mobile."_code");
            Code::check_code($key,$code);
            $user->mobile     = $mobile;
        }

        if($email){
            $key = md5($email."_code");
            Code::check_code($key,$code);
            $user->email     = $email;
        }

        $user->save();

        Export::exp_ok();
    }
    /** 102 关注用户 */
    public function focus_on_user(){
        $data = Request::post(["user_id","state"]);
        if($data['user_id'] == $this->user_id) throw new Exception("无法关注自己");

        $user = UserModel::get($data['user_id']);
        if(!$user) throw new Exception("用户不存在");

        // 关注用户状态
        $operation1 = User_operation::where([
                                            "user_id"=>$this->user_id,
                                            "object_user_id"=>$data['user_id'],
                                        ])->find();
        // 被关注状态
        $operation2 = User_operation::where([
                                            "object_user_id"=>$this->user_id,
                                            "user_id"=>$data['user_id'],
                                        ])->find();

        if($data['state']){
            // 关注
            if(!$operation1){
                //未关注
                User_operation::create([
                    "user_id"=>$this->user_id,
                    "object_user_id"=>$data['user_id'],
                    "type"=>0,
                ]);
            }
        }else{
            // 取消关注
            if($operation1){
                $operation1->delete();
                // 是否存在互相关注
                if($operation2 && $operation2->type == 1){
                    $operation2->type =0;
                    $operation2->save();
                }
            }
        }

        Export::exp_ok();
    }

    /** 103 获取websokcet登录token  */
    private function get_socket_token(){
        $token = WebSocketToken::set($this->user_id);
        $info = [
            "token"=>$token
        ];
        Export::exp_ok($info);
    }
    /** 获取关注列表 */
    public function get_focus_user(){
        $data = Request::post([]);

        $keyword = isset($data['keyword'])?$data['keyword']:"";

        $user_list = User_operation::join("user u","u.id=uo.object_user_id")
            ->alias('uo')
            ->where([
                ['uo.user_id','=',$this->user_id],
                ["u.name","like","{$keyword}%"]
            ])->field("u.id,u.name,u.pic_url")
            ->order("name","asc")
            ->select();
        $data = [
            "data"=>$user_list,
        ];
        Export::exp_ok($data);
    }
}